package com.starsoft.frame.base.web;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.base.bean.BaseMenu;
import com.starsoft.frame.base.bean.BaseUser;
import com.starsoft.frame.base.service.MenuService;
import com.starsoft.frame.base.service.UserService;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.frame.util.JsonTreeUtil;
import com.starsoft.frame.util.JsonTreeUtil.TreeNode;


@RestController
@RequestMapping(value = "/base/menu")
public class MenuController {

	@Autowired
	MenuService ms=null;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/query")
	public Set<TreeNode> query(HttpServletRequest request) {
		BaseUser user= UserService.getUserContext().getUser();
		List menus=ms.getAllMenu(user);
		return JsonTreeUtil.getTree(menus, "code", "pcode", "sort","name");
	}
	
	@RequestMapping(value = "/getbycode")
	public BaseMenu getByCode(HttpServletRequest request){
		String code=request.getParameter("code");
		return ms.get(code);
	}
	
	@RequestMapping(value = "/save")
	public Map<String, Object> save(BaseMenu menu,HttpServletRequest request){
		ms.saveOrUpdate(menu);
		return WebUtil.sucesseModel("保存成功！");
	}
	
	@RequestMapping(value = "/delete")
	public  Map<String, Object> delete(BaseMenu menu){
		ms.delete(menu);
		return WebUtil.sucesseModel("删除成功！");
	}
}
